#region Using

using Microsoft.Xna.Framework;
using XNgine.Misc;
using XNgine.Services;

#endregion

namespace XNgine.CameraManagement
{
    public class CameraManager : GameComponent, ICameraManagement
    {
        public CameraManager()
            : base(XEngine.Instance)
        {
        }

        private readonly NamedItemCollection<ICamera> _cameras = new NamedItemCollection<ICamera>();

        public NamedItemCollection<ICamera> Cameras
        {
            get { return _cameras; }
        }

        private ICamera _activeCamera;

        public ICamera ActiveCamera
        {
            get { return _activeCamera; }
            set { _activeCamera = value; }
        }

        public void AddCamera(ICamera camera)
        {
            _cameras.Add(camera);

            if (ActiveCamera == null)
                ActiveCamera = camera;
        }

        public void RemoveCamera(ICamera camera)
        {
            _cameras.Remove(camera);
            if (_activeCamera == camera)
                _activeCamera = _cameras.GetLast();
        }

        public override void Update(GameTime gameTime)
        {
            if (_activeCamera != null)
                _activeCamera.Update();
        }
    }
}